package com.mjh.listener;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.mjh.comm.Constant;


public class ContextListener implements ServletContextListener {

	public static Logger log = Logger.getLogger(ContextListener.class);
	
    public ContextListener() {
      
    }


    public void contextInitialized(ServletContextEvent arg0) {
        String prefix = arg0.getServletContext().getRealPath("/");
        
        Constant.SERVLET_CONTEXT = prefix;
        log.info("系统根目录：" + prefix);
        
        String file = "WEB-INF/classes/log4j.properties";
        String filePath = prefix + file;
        Properties props = new Properties();
        try {
            FileInputStream istream = new FileInputStream(filePath);
            props.load(istream);
            istream.close();    
            String logFile = prefix + props.getProperty("log4j.appender.R.File");//设置路径
            log.info("日志文件路径：" + logFile);
            props.setProperty("log4j.appender.R.File",logFile);
            PropertyConfigurator.configure(props);//装入log4j配置信息
        } catch (IOException e) {
        	log.error("Could not read configuration file [" + filePath + "].");
        	log.error("Ignoring configuration file [" + filePath + "].");
            return;
        }
    }


    public void contextDestroyed(ServletContextEvent arg0) {
      
    }
	
}
